package com.atguigu.test.dacahngmainshitidierji.threadpool;

import java.util.concurrent.TimeUnit;

class   HoldLockThread implements Runnable{

    private String locka;

    private String lockb;

    public HoldLockThread(String locka, String lockb) {
        this.locka = locka;
        this.lockb = lockb;
    }

    @Override
    public void run() {
        synchronized (locka){
            System.out.println(Thread.currentThread().getName() + "\t 自己持有：" + locka + "\t 尝试获取：" + lockb);
            try {
                TimeUnit.SECONDS.sleep(2);
            }catch (Exception e){
                e.printStackTrace();
            }
            synchronized (lockb){
                System.out.println(Thread.currentThread().getName() + "\t 自己持有：" + lockb + "\t 尝试获取：" + locka);
            }
        }
    }
}
/**
 * @Author: 19shusheng.com
 * @Description:
 * @Date: Created in 21:19 2020/11/18
 * @Modified By:
 */
public class DeadLock {
    //死锁代码
    public static void main(String[] args) {
        String locka = "locka";
        String lockb = "lockb";
        new Thread(new HoldLockThread(locka,lockb),"线程1").start();
        new Thread(new HoldLockThread(lockb,locka),"线程2").start();
        /*linux ps -ef | grep ×      ls -l
        * windows下的java运行程序也有类似ps查看进程的命令，但目前我们需要查看的只是java
        *  jps =  java ps            jps -l
        * */
    }

}
